#include "wrr.h"
#include "rectype.h"
#include "util.h"
#include "nmfield.h"
#include "nmfield.cpp"

#include "cnfield.h"

#include "stdffield.h"
#include "stdffield.cpp"

using namespace std;
using namespace opsrc::stdf;

Wrr::Wrr()
{}


Wrr::Wrr(stdf_rec _stdfrec, int _reclen, std::ios::pos_type _fileRecFirstBytePosition, int _orderindex, endian e, char* buf )
:RecBase(_stdfrec, _reclen, _fileRecFirstBytePosition, _orderindex),en(e)
{
    if (buf!=0)
        readFields(buf);
}


Wrr::~Wrr() {
}

const StdfField<int> Wrr::getHeadNum() const {
    return head_num;
}

void Wrr::setHeadNum(StdfField<int> headnum) {
    head_num = headnum;
}

const StdfField<int> Wrr::getSiteGroup() const {
    return site_grp;
}

void Wrr::setSiteGroup(StdfField<int> sitegrp) {
    site_grp = sitegrp;
}

const StdfField<long> Wrr::getFinishTime() const {
    return finish_t;
}

void Wrr::setFinishTime(StdfField<long> finisht) {
    finish_t = finisht;
}

const StdfField<long> Wrr::getPartCount() const {
    return part_cnt;
}

void Wrr::setPartCount(StdfField<long> partcnt) {
    part_cnt = partcnt;
}

const StdfField<long> Wrr::getRetestCount() const {
    return rtst_cnt;
}

void Wrr::setRetestCount(StdfField<long> rtstcnt) {
    rtst_cnt = rtstcnt;
}

const StdfField<long> Wrr::getAbortCount() const {
    return abrt_cnt;
}

void Wrr::setAbortCount(StdfField<long> abrtcnt) {
    abrt_cnt = abrtcnt;
}

const StdfField<long> Wrr::getGoodCount() const {
    return good_cnt;
}

void Wrr::setGoodCount(StdfField<long> goodcnt) {
    good_cnt = goodcnt;
}

const StdfField<long> Wrr::getFunctionalCount() const {
    return func_cnt;
}

void Wrr::setFunctionalCount(StdfField<long> funccnt) {
    func_cnt = funccnt;
}

const StdfField<string> Wrr::getWaferId() const {
    return wafer_id;
}

void Wrr::setWaferId(StdfField<string> waferid) {
    wafer_id = waferid;
}

const StdfField<string> Wrr::getFabWaferId() const {
    return fabwf_id;
}

void Wrr::setFabWaferId(StdfField<string> fabwfid) {
    fabwf_id = fabwfid;
}

const StdfField<string> Wrr::getFrameId() const {
    return frame_id;
}

void Wrr::setFrameId(StdfField<string> frameid) {
    frame_id = frameid;
}

const StdfField<string> Wrr::getMaskId() const {
    return mask_id;
}

void Wrr::setMaskId(StdfField<string> maskid) {
    mask_id = maskid;
}

void Wrr::setUserDescription(StdfField<string> usrdesc) {
    usr_desc = usrdesc;
}

const StdfField<string> Wrr::getUserDescription() const {
    return usr_desc;
}

void Wrr::setExecDescription(StdfField<string> excrdesc) {
    excr_desc = excrdesc;
}

const StdfField<string> Wrr::getExecDescription() const {
    return excr_desc;
}

void Wrr::readFields(char *buf) {

#if _DEBUG
    cout << "stdfrec= WRR , filebytestartposition= " <<   getFileRecByteStartPosition() << ", recbytecount= " << reclen <<endl;
#endif

    int curpos = 0;
    // Read head number , 1 byte
    NmField<unsigned int, 1> headnum(buf+curpos, en);
    head_num.set(headnum.getData(), curpos, headnum.getByteCount(), headnum.getTotalByteCount());
    curpos += head_num.getTotalByteCount();


    // Read site group , 1 byte
    if (curpos < reclen -1) {
        NmField<unsigned int, 1> sitegrp(buf+curpos, en);
        site_grp.set(sitegrp.getData(), curpos, sitegrp.getByteCount(), sitegrp.getTotalByteCount());
        curpos += site_grp.getTotalByteCount();
    }



    // Read site group , 1 byte
    if (curpos < reclen -1) {
        NmField<long, 4> finisht(buf+curpos, en);
        finish_t.set(finisht.getData(), curpos, finisht.getByteCount(), finisht.getTotalByteCount());
        curpos += finish_t.getTotalByteCount();
    }


    // read part count
    if (curpos < reclen -1 ) {
        NmField<long, 4> partcnt(buf+curpos, en);
        part_cnt.set(partcnt.getData(), curpos, partcnt.getByteCount(), partcnt.getTotalByteCount());
        curpos += part_cnt.getTotalByteCount();
    }



    // read retest count
    if (curpos < reclen-1) {
        NmField<long, 4> rtstcnt(buf+curpos, en);
        rtst_cnt.set(rtstcnt.getData(), curpos, rtstcnt.getByteCount(), rtstcnt.getTotalByteCount());
        curpos += rtst_cnt.getTotalByteCount();
    }


    // read abort count
    if (curpos < reclen-1) {
        NmField<long, 4> abrtcnt(buf+curpos, en);
        abrt_cnt.set(abrtcnt.getData(), curpos, abrtcnt.getByteCount(), abrtcnt.getTotalByteCount());
        curpos += abrt_cnt.getTotalByteCount();
    }


    // read good count
    if (curpos < reclen -1) {
        NmField<long, 4> goodcnt(buf+curpos, en);
        good_cnt.set(goodcnt.getData(), curpos, goodcnt.getByteCount(), goodcnt.getTotalByteCount());
        curpos += good_cnt.getTotalByteCount();
    }


    // read functional count
    if (curpos < reclen -1) {
        NmField<long, 4> funccnt(buf+curpos, en);
        func_cnt.set(funccnt.getData(), curpos, funccnt.getByteCount(), funccnt.getTotalByteCount());
        curpos += func_cnt.getTotalByteCount();
    }

    
    // read waferid
    if (curpos < reclen -1) {
        CnField<string> waferid(buf+curpos, en);
        wafer_id.set(waferid.getData(), curpos, waferid.getByteCount(), waferid.getTotalByteCount());
        curpos += wafer_id.getTotalByteCount();
    }


    // read fab wafer id
    if (curpos < reclen-1) {
        CnField<string> fabwfid(buf+curpos, en);
        fabwf_id.set(fabwfid.getData(), curpos, fabwfid.getByteCount(), fabwfid.getTotalByteCount());
        curpos += fabwf_id.getTotalByteCount();
    }

    // read frame id
    if (curpos < reclen-1) {
        CnField<string> frameid(buf+curpos, en);
        frame_id.set(frameid.getData(), curpos, frameid.getByteCount(), frameid.getTotalByteCount());
        curpos += frame_id.getTotalByteCount();
    }

    // read mask id
    if (curpos < reclen -1) {
        CnField<string> maskid(buf+curpos, en);
        mask_id.set(maskid.getData(), curpos, maskid.getByteCount(), maskid.getTotalByteCount());
        curpos += mask_id.getTotalByteCount();
    }

    // read user description
    if ( curpos < reclen -1) {
        CnField<string> usrdesc(buf+curpos, en);
        usr_desc.set(usrdesc.getData(), curpos, usrdesc.getByteCount(), usrdesc.getTotalByteCount());
        curpos += usr_desc.getTotalByteCount();
    }

    // read execute description
    if ( curpos < reclen -1) {
        CnField<string> excrdesc(buf+curpos, en);
        excr_desc.set(excrdesc.getData(), curpos, excrdesc.getByteCount(), excrdesc.getTotalByteCount());
    }
}
